import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_type_config.dart';
import 'package:largefriends/entity/entities/lf_location_entity.dart';
import 'package:largefriends/res.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/binary_util.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';
import 'package:largefriends/widgets/lf_location_widget.dart';
import 'package:largefriends/widgets/lf_multi_picker.dart';
import 'package:largefriends/widgets/lf_single_picker.dart';

import 'lf_search_filter_logic.dart';

class LfSearchFilterPage extends StatefulWidget {
  const LfSearchFilterPage({super.key});

  @override
  State<LfSearchFilterPage> createState() => _LfSearchFilterPageState();
}

class _LfSearchFilterPageState extends State<LfSearchFilterPage> {
  final logic = Get.put(LfSearchFilterLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: lfAppBar(
          leadingCallback: () {
            Get.back();
          },
          title: strFilters.tr,
          centerTitle: false,
          actions: [
            Obx(() {
              return InkWell(
                onTap: () {
                  if (logic.enable) {
                    logic.save();
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  child: Text(
                    strSave.tr,
                    style: lfTextStyle(
                        color: logic.enable
                            ? colorFF007F
                            : Colors.transparent,
                        fontFamily: fontBold,
                        sp: 18.sp),
                  ),
                ),
              );
            })
          ],
        ),
        body: SingleChildScrollView(
          physics: const BouncingScrollPhysics(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Text(
                  strShowMe.tr,
                  style: lfTextStyle(
                      sp: 17.sp, fontFamily: fontBold,color: color514F62),
                ),
              ),
              10.h.sbh,
              Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: InkWell(
                    onTap: () {
                      showLfMultiPickerSheet(configTypeEnum: LfConfigTypeEnum.gender, value: logic.gender.value,result: (int result) {
                        if(result != 0){
                          logic.gender.value = result;
                        }
                      },mustSelected: true,needPass: false,needPlural: true);
                    },
                    child: Container(
                      decoration: BoxDecoration(
                          color: color8F8F8F, borderRadius: 5.r.radius),
                      padding:
                      EdgeInsets.symmetric(horizontal: 6.w, vertical: 12.h),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Icon(
                            Icons.group_outlined,
                            color: colorFF007F,
                            size: 20.w,
                          ),
                          8.w.sbw,
                          Expanded(
                            child: Obx(() {
                              return Text(
                                  TypeConfig().getValueByKey(LfConfigTypeEnum.gender, logic.gender.value,needPlural: true),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: lfTextStyle(
                                    fontFamily: fontMedium, sp: 16.sp),
                              );
                            }),
                          )
                        ],
                      ),
                    ),
                  )),
              30.h.sbh,
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Row(
                  children: [
                    Expanded(
                      child: Text(
                        strAgeRange.tr,
                        style: lfTextStyle(
                            sp: 17.sp,
                            fontFamily: fontBold,color: color514F62),
                      ),
                    ),
                    Obx(() => Text(
                          "${logic.minAge.value} - ",
                          style: lfTextStyle(sp: 18.sp, fontFamily: fontMedium),
                        )),
                    Container(
                      width: 30.w,
                      alignment: Alignment.centerLeft,
                      child: Obx(() => Text(
                            "${logic.maxAge.value}",
                            style:
                                lfTextStyle(sp: 18.sp, fontFamily: fontMedium),
                          )),
                    )
                  ],
                ),
              ),
              _ageSlider(),
              20.h.sbh,
              Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Text(
                    strLocation.tr,
                    style: lfTextStyle(
                        sp: 17.sp, fontFamily: fontBold,color: color514F62),
                  )),
              10.h.sbh,
              Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: InkWell(
                    onTap: () {
                      showLfLocationWidget(
                        initLocation: logic.location.value ??
                            LfLocationEntity(
                                countryId: '0', country: "AnyWhere"),
                        showAnyWhere: true,
                        passCity: true,
                        onConfirm: (locationEntity) {
                          if (locationEntity.countryId == '0' ||
                              locationEntity.countryId.isNullOrEmpty) {
                            logic.location.value = null;
                          } else {
                            logic.location.value = LfLocationEntity();
                            logic.location.value!.countryId =
                                locationEntity.countryId;
                            logic.location.value!.country =
                                locationEntity.country;
                            logic.location.value!.countAbbr =
                                locationEntity.countAbbr;
                            if (locationEntity.stateId != "0" &&
                                !locationEntity.stateId.isNullOrEmpty) {
                              logic.location.value!.stateId =
                                  locationEntity.stateId;
                              logic.location.value!.state =
                                  locationEntity.state;
                              logic.location.value!.stateAbbr =
                                  locationEntity.stateAbbr;
                              if (locationEntity.cityId != '0' &&
                                  !locationEntity.cityId.isNullOrEmpty) {
                                logic.location.value!.cityId =
                                    locationEntity.cityId;
                                logic.location.value!.city =
                                    locationEntity.city;
                              }
                            }
                          }
                          logic.location.refresh();
                        },
                      );
                    },
                    child: Container(
                      decoration: BoxDecoration(
                          color: color8F8F8F, borderRadius: 5.r.radius),
                      padding:
                          EdgeInsets.symmetric(horizontal: 6.w, vertical: 12.h),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Icon(
                            Icons.location_on,
                            color: colorFF007F,
                            size: 20.w,
                          ),
                          8.w.sbw,
                          Expanded(
                            child: Obx(() {
                              return Text(
                                logic.location.value?.toAddress() ?? 'AnyWhere',
                                style: lfTextStyle(
                                    fontFamily: fontMedium, sp: 16.sp),
                              );
                            }),
                          )
                        ],
                      ),
                    ),
                  )),
              15.h.sbh,
            ],
          ),
        ),
      ),
      onWillPop: () async {
        Get.back();
        return false;
      },
    );
  }

  Widget _ageSlider() {
    return Obx(() {
      return RangeSlider(
        values: RangeValues(
            logic.minAge.value.toDouble(), logic.maxAge.value.toDouble()),
        max: 99,
        min: 18,
        onChanged: (value) {
          logic.minAge.value =
              value.start.toInt() < 19 ? 19 : value.start.toInt();
          logic.maxAge.value = value.end.toInt() > 99 ? 99 : value.end.toInt();
        },
        activeColor: colorFF007F,
        inactiveColor: color8F8F8F,
      );
    });
  }

  Widget _genderItem({
    required String title,
    required int value,
  }) {
    return InkWell(
      onTap: () {
        bool isSelected = containKey(value, logic.gender.value);
        if (isSelected) {
          logic.gender.value = logic.gender.value - value;
        } else {
          logic.gender.value = logic.gender.value + value;
        }
      },
      child: Obx(() {
        bool isSelected = containKey(value, logic.gender.value);
        return Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: isSelected ? colorFF007F : color8F8F8F,
            borderRadius: 5.r.radius,
          ),
          height: 40.h,
          width: MediaQuery.of(context).size.width / 100 * 35,
          child: Text(
            title,
            style: lfTextStyle(
                sp: 16.sp,
                fontFamily: fontMedium,
                color: isSelected ? colorFEFEFE : colorFEFEFE),
          ),
        );
      }),
    );
  }
}
